package com.gitee.wsl.lifecycle.nav

interface SelectAbleList<V,T: SelectAbleList<V,T>> {

    val items:List<V>

    val selectedIndex: Int

    fun isEmpty():Boolean  = items.isEmpty()

    fun copyOf(selectedIndex: Int)= copyOf(items = items,selectedIndex=selectedIndex)

    fun copyOf(items:List<V>, selectedIndex: Int):T

    fun clear():T = copyOf(items = emptyList(), selectedIndex = -1)
}

fun<V,T: SelectAbleList<V,T>> SelectAbleList(items:List<V> = emptyList(), selectedIndex:Int = -1) = SelectAbleListImp(items ,selectedIndex)


data class SelectAbleListImp<C>(
      override val items: List<C>,
      override val selectedIndex: Int,
   ): SelectAbleList<C,SelectAbleListImp<C>> {
    /**
     * Creates empty [SelectAbleList].
     */
    constructor() : this(items = emptyList(), selectedIndex = -1)

    override fun copyOf(
        items: List<C>,
        selectedIndex: Int,
    ) = copy(items = items,selectedIndex=selectedIndex)

    init {
        if (items.isNotEmpty()) {
            require(selectedIndex in items.indices) {
                "The selectedIndex argument must be with the range: ${items.indices}. Actual: $selectedIndex."
            }
        }
    }
}